<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\CouponConfig;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CouponConfigController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CouponConfig(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('terms_of_use');
            $grid->column('threshold_of_use');
            $grid->column('quota')->display(function ($var) {
                return $var / 100;
            });
            $grid->column('deduction_ratio')->display(function ($var) {
                return $var ."%";
            });
            $grid->column('valid_hour');
            $grid->column('state')->switch();


            $grid->disableRowSelector();
            $grid->disableViewButton();
            $grid->showQuickEditButton();
            $grid->disableFilterButton();

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CouponConfig(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('terms_of_use');
            $show->field('threshold_of_use');
            $show->field('quota');
            $show->field('deduction_ratio');
            $show->field('valid_hour');
            $show->field('state');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CouponConfig(), function (Form $form) {

            $form->text('title');
            $form->text('terms_of_use');
            $form->text('threshold_of_use');

            $form->model()->quota = $form->model()->quota / 100;
            $form->text('quota')->saving(function ($var) {
                $var = $var * 100;
                return $var;
            });

            $form->text('deduction_ratio')->help("50%,填50");
            $form->text('valid_hour')->help('输入整数小时:比如 10');
            $form->switch('state');


        });
    }
}
